package com.tm.controller;


import com.tm.model.Role;
import com.tm.service.RoleService;
import com.tm.util.RestReturnModel;
import com.tm.vo.RoleVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "查询人员")
    @GetMapping
    public RestReturnModel queryAllRole(RoleVo roleVo){
        RoleVo list = roleService.queryAllRole(roleVo);
        return RestReturnModel.SUCCESS(list);
    }
    @PutMapping("/{id}/{status}")
    public RestReturnModel statusChange(@PathVariable Integer id,@PathVariable Integer status){
        roleService.statusChange(id,status);
        return RestReturnModel.SUCCESS(null);
    }
    //新增
    @ApiOperation(value = "新增角色",notes = "新增角色" )
    @PostMapping
    public RestReturnModel addRole(@RequestBody Role role){
        roleService.addRole(role);
        return RestReturnModel.SUCCESS(null);
    }

    @ApiOperation(value = "修改角色",notes = "修改角色" )
    @PutMapping
    public Map<String,Object> updateRole(@RequestBody Role role){
        Map<String, Object> map = new HashMap<>();
        try {
            roleService.updateRole(role);
            map.put("flag", 200);
        }catch (Exception e){
            e.printStackTrace();
            map.put("flag",202);
        }
        return  map;
    }
    //回显
    @ApiOperation(value = "回显选中数据",notes = "回显选中数据" )
    @ApiImplicitParam(name = "id",value = "角色id",required = true,dataType = "number")
    @GetMapping("{id}")
    public RestReturnModel queryRoleById(@PathVariable Integer id){

        return  RestReturnModel.SUCCESS(roleService.queryRoleById(id));
    }

    //删除
    @DeleteMapping("/{id}")
    public RestReturnModel deleteRoleId(@PathVariable Integer id){
            roleService.deleteRoleId(id);
        return RestReturnModel.SUCCESS(null);
    }
    @ApiOperation(value = "导出")
    @GetMapping("exportFile")
    public void exportFile(HttpServletResponse response ){
        roleService.exportFile(response);
    }

    @ApiOperation(value = "导入")
    @PostMapping ("uploadFile")
    public void uploadFile(MultipartFile dataFile) {
        roleService.uploadFile(dataFile);
    }


}
